package com.wangyazhu.MybatisDemo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Data
@AllArgsConstructor
@Api(tags = "用户管理")
@RestController
@RequestMapping("/")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 根据用户name查询用户信息
     * @param name
     * @return
     */
    @ApiOperation(value = "根据用户名获取用户信息")
    @GetMapping("/user/{name}")
    public User findByName(@PathVariable String name) {
        return userMapper.findByName(name);
    }

    /**
     * 保存用户信息到数据库
     * @param user
     * @return
     */
    @ApiOperation(value = "根据用户信息创建用户")
    @PostMapping("/create")
    public int save(@RequestBody User user) {
        return userMapper.insert(user.getName(), user.getAge());
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @ApiOperation(value = "获取所有用户信息")
    @GetMapping("/user/all")
    public List<User> getUserAll() {
        return userMapper.findAll();
    }

    /**
     * 更新用户信息
     * @param user
     */
    @ApiOperation(value = "更新用户信息", notes = "根据用户名修改用户年龄")
    @PostMapping("/update")
    public void updateUser(@RequestBody User user) {
        userMapper.update(user);
    }

    /**
     * 根据用户id删除用户信息
     * @param id
     */
    @ApiOperation(value = "根据ID删除用户信息")
    @DeleteMapping("/delete/{id}")
    public void deleteUserById(@PathVariable Long id) {
        userMapper.delete(id);
    }
}
